/*
 * CastDRAndroid
 * Copyright (c) 2014 Christian Holm Christensen
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 *
 */
package org.cholm.media.castdr.backend;


import org.cholm.media.castdr.LogUtils; import static org.cholm.media.castdr.LogUtils.LOGd;
/**
 * @author cholm
 */
public class Search extends SubCategory {

    private static final String TAG = LogUtils.makeLogTag(Search.class);
    /**
     * List of genres
     */
    private String[] mGenres = null;
    /**
     * Whether to search by start or content
     */
    private boolean mStartNotContain = true;
    /**
     * Title to search for
     */
    private String mTitle = "";
    /**
     * Genre to search for
     */
    private String mGenre = "";
    /**
     * Channel
     */
    private String mChannel = "";

    /**
     * @param c
     * @param name
     * @param url
     */
    public Search(Category c, Category genres) {
        super(c, "", "");
        mGenres = new String[genres.getMap().size()];
        int i = 0;
        for (SubCategory s : genres.getMap().values()) {
            mGenres[i] = s.getName();
        }
    }

    /**
     * We always return false here
     *
     * @return false
     */
    @Override
    public boolean isLoaded() {
        return false;
    }

    public void parse(String query) {
        mStartNotContain = true;
        mTitle = "";
        mGenre = "";
        mChannel = "";
        if (query.indexOf("=") < 0) {
            LOGd(TAG, "No fields set, assuming title");
            mTitle = query.trim();
            return;
        }

        String title = "";
        String genre = "";
        String channel = "";
        String[] parts = query.split("&");
        LOGd(TAG, "Got " + parts.length + " parts");
        for (int i = 0; i < parts.length; i++) {
            String part = parts[i].trim();
            String[] pair = part.split("=");
            LOGd(TAG, "Part # " + i + ": " + part + " [" + pair[0] + ","
                    + pair[1] + "]");
            if (pair[0].equalsIgnoreCase("titel")
                    || pair[0].equalsIgnoreCase("title")) {
                title = pair[1];
            } else if (pair[0].equalsIgnoreCase("indholder")
                    || pair[0].equalsIgnoreCase("contains")) {
                mStartNotContain = false;
                if (pair[1].length() > 0) title = pair[1];
            } else if (pair[0].equalsIgnoreCase("start")) {
                mStartNotContain = true;
                if (pair[1].length() > 0) title = pair[1];
            } else if (pair[0].equalsIgnoreCase("genre")) {
                genre = pair[1];
            } else if (pair[0].equalsIgnoreCase("channel")
                    || pair[0].equalsIgnoreCase("kanal")) {
                channel = pair[1];
            } else title = pair[0];
        }
        mTitle = title.trim();
        mGenre = genre.trim();
        mChannel = channel.trim();
        LOGd(TAG, "Parsed " + (mStartNotContain ? "start" : "contain")
                + " title=" + mTitle + " genre=" + mGenre + " channel="
                + mChannel);
    }

    /**
     * Overload the load function
     */
    @Override
    public boolean load() {
        Extractor e = Extractor.instance();
        boolean byDate = (mTitle.length() > 0) ? false : true;
        mUrl = e.queryUrl(mStartNotContain, mTitle, mGenre, mChannel, false,
                byDate, 100, 0);
        clear();
        LOGd(TAG, "Will do search on " + mUrl);
        boolean ret = super.load();
        mLoaded = false;
        return ret;
    }
}
